package com.example.bbgds.test;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

public class VoiceActivity extends Activity {

    private FrameLayout mLayout;
    private LayoutBottom bottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLayout = (FrameLayout) findViewById(R.id.viewpager);
        bottom = (LayoutBottom) findViewById(R.id.bottom);

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        final DataItem item = new DataItem();
        item.mType = DataItem.TYPE_TEXT;
        item.isRight = true;
        item.isVisible = false;
//        item.mText = "你好吗奥哦嗷嗷啊\n你好吗奥哦嗷嗷啊";
        bottom.addItemToList(item);
        TextView view = getTextView(this, "你好吗奥哦嗷嗷啊\n你好吗奥哦嗷嗷啊");
        mLayout.addView(view, lp);
        view.setTag(item);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startAnimation(v);
            }
        });
    }

    private void startAnimation(final View view) {
        int[] location = new int[2];
        int targetHeight = bottom.getRecyclerView().getLayoutManager().getChildAt(1).getHeight();
        bottom.getRecyclerView().getLayoutManager().getChildAt(1).getLocationInWindow(location);
        int endY = location[1] - (view.getHeight() - targetHeight);

        view.getLocationInWindow(location);
        int startY = location[1];
        ObjectAnimator translationX;
        ObjectAnimator translationY = ObjectAnimator.ofFloat(view, "translationY", 0, (endY - startY));
        translationX = ObjectAnimator.ofFloat(view, "translationX", 0, mLayout.getWidth() - view.getWidth() - view.getX());
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.5f);
        AnimatorSet s1 = new AnimatorSet();
        s1.setDuration(2000);
        s1.playTogether(translationX, alpha, translationY);
        s1.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                end(view);
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                end(view);
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        s1.start();
    }


    private void end(View view) {
        DataItem item = (DataItem) view.getTag();
        item.isVisible = true;
        item.mText = "你好吗奥哦嗷嗷啊\n你好吗奥哦嗷嗷啊";
        bottom.getRecyclerView().getAdapter().notifyDataSetChanged();
        mLayout.removeView(view);
        mLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                addTag();
                findViewById(R.id.addvoice).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        addItem();
                    }
                });
            }
        }, 1500);
    }

    private Random random = new Random();

    private void addItem() {
        if (random.nextInt(10) >= 5) {
            addText();
        } else {
            addTag();
        }
    }

    boolean isRight = true;

    private void addText(){
        String text = "帮我查一下去上海的机票";
        TextView textView = getTextView(this, text);
        addItem(DataItem.TYPE_TEXT, text, textView);
    }

    public void addTag(){
        String text = "测试时间你号码和\n发卡第三方";
        View textView = View.inflate(this, R.layout.layout_tag, null);
        addItem(DataItem.TYPE_TAG, text, textView);
    }


    private void addItem(int type, String text, View view){
        final DataItem item = new DataItem();
        item.mType = type;
        item.mText = text;
        isRight = !isRight;
        item.isRight = isRight;
        int height = 0;
        if (view instanceof TextView) {
            height = 105;
        } else {
            height = 310;
        }
        bottom.addItem(view, item, height);
    }

    public static TextView getTextView(Context context, String text){
        TextView textView = new TextView(context);
        textView.setText(text);
        textView.setTextColor(0xFFFFFFFF);
        textView.setBackgroundColor(0xFF256699);
        textView.setTextSize(16);
        textView.setPadding(0, 40, 0, 0);
        return textView;
    }
}
